'''
Created on Jul 21, 2011

@author: Krystian Brachmanski
'''

from basicStatistics import MaxValueStatistics
from basicStatistics import ExecutionStatistics
    

class blacklistHandler:

    """
    Class for handling the blacklist of processes and calculate statistics for max CPU and memory utilization as well as number of execution
    
    @author: Krystian Brachmanski
    @require: basicStatistics (MaxValueStatistics, ExecutionStatistics)
    """
            
    def __init__(self,blacklist):
        """
        Default constructor - gets the list of blacklisted processes as the input parameter
        @param blacklist: the list of blacklisted processes as the input parameter
        """
        self.cpuMaxStats = MaxValueStatistics()
        self.memMaxStats = MaxValueStatistics()
        self.execStats = ExecutionStatistics()
        self.blacklist = blacklist[:]
        self.prog_list = [ ]
    
    def checkIfBlacklisted(self,name):
        """
        Check if the process name is currently on the blacklist.
        @param name: Name of the process to be checked 
        @return: True if it is blacklisted, False if it is not
        """
        return any(s in name for s in self.blacklist) 

    def getIndexInBlacklist(self,name):
        """
        Get the index within the blacklist for the process name passed as the parameter
        @param name: the name of the item/process to be checked  
        @return: the index within blacklist
        """
        return [s for s in self.blacklist if s in name][0]
                 
    def processItem(self,name):
        """
        check if the process is on the blacklist
        @param name: the name of the process to be checked against the blacklist and increase the statistics for it
        """
        blacklisted = self.checkIfBlacklisted(name)
        if blacklisted:
            if name not in self.prog_list:
                self.execStats.addNewItem(self.getIndexInBlacklist(name))
                self.prog_list.append(name)
        return blacklisted
    
    def getStatistics(self):
        """
        @return: the execution statistics in the form of dictionary (blacklisted process name group <-> execution stat)
        """
        return self.execStats.getStatistics()
    
    def addNewCpuItem(self,name,value):
        """
        Add new CPU value to the statistics - currently only maximal value is checked
        @param name: the name of the process/item
        @param value: the value associated with the process/item
        """
        return self.cpuMaxStats.addNewItem(self.getIndexInBlacklist(name), value)
    
    def addNewMemItem(self,name,value):
        """
        Add new memory utilization value to the statistics - currently only maximal value is checked
        @param name: the name of the process/item
        @param value: the value associated with the process/item
        @return: @see: memMaxStats.addNewItem
        """
        return self.memMaxStats.addNewItem(self.getIndexInBlacklist(name), value)
    
    def getCpuStatistics(self):
        """
        @return: CPU usage statistics in the form of dictionary (blacklisted process name group <-> max CPU usage)
        """
        return self.cpuMaxStats.getStatistics()
    
    def getMemStatistics(self):
        """
        @return: memory usage statistics in the form of dictionary (blacklisted process name group <-> max memory usage)
        """
        return self.memMaxStats.getStatistics()
